﻿Imports SecurityService

Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_KhachSan_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "regjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "regjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            BindHotelType()
        End If
    End Sub
    Protected Sub BindHotelType()
        'gen CodeHotal

        Using _data As New VSEntities
            'huyen
            Dim lstHuyen = (From q In _data.Provinces Where q.ParentId = 232 Order By q.Title Select q.Id, q.Code, q.Title).ToList
            With ddlDistrict
                .DataSource = lstHuyen
                .DataTextField = "Title"
                .DataValueField = "Code"
                .DataBind()
                Dim itm As New ListItem("--- Chọn ---", "")
                .Items.Insert(0, itm)
            End With
            'gen CodeHotel
            Dim _dt As Date = Now.Date
            Dim ht As Integer = (From q In _data.Hotels Where q.Created > _dt Order By q.Id Descending Select q).Count + 1

            txtHotelCode.Text = Now.Date.ToString("yyMMdd").ToString & ht.ToString.PadLeft(3, "0")
            hidCodeHotel.Text = Now.Date.ToString("yyMMdd").ToString & ht.ToString.PadLeft(3, "0")

            Dim p = (From q In _data.HotelTypes Select q).ToList
            With ddlHoteltypeid
                .DataTextField = "Title"
                .DataValueField = "HotelTypeId"
                .DataSource = p
                .DataBind()
                Dim itm As New ListItem("--- Chọn ---", "")
                .Items.Insert(0, itm)
            End With
        End Using
    End Sub
#End Region


#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Using data As New VSEntities
            Dim k = (From q In data.Hotels Where q.HotelCode = txtHotelCode.Text Select q).FirstOrDefault
            If Not k Is Nothing Then
                Excute_Javascript("Mã CSLT đã tồn tại trong hệ thống", Me.Page)
                Exit Sub
            End If

            Dim p As New VSModel.Hotel
            Try
                p.HotelName = txtHotelname.Text.Trim()
                p.HotelCode = txtHotelCode.Text.Trim()
                p.HotelTypeId = ddlHoteltypeid.SelectedValue()
                p.Address = txtAddress.Text.Trim()
                p.BusinessLicence = txtBusinesslicence.Text.Trim()

                Dim _NumberOfRoom As Integer
                Try
                    _NumberOfRoom = CInt(txtNumberofroom.Text.Trim())
                Catch ex As Exception
                    _NumberOfRoom = 0
                End Try

                Dim _NumberOfStaff As Integer
                Try
                    _NumberOfStaff = CInt(txtNumberofstaff.Text.Trim())
                Catch ex As Exception
                    _NumberOfStaff = 0
                End Try

                p.NumberOfRoom = _NumberOfRoom
                p.NumberOfStaff = _NumberOfStaff
                p.Ward = ddlWard.SelectedValue
                p.District = ddlDistrict.SelectedValue

                p.Director = txtDirector.Text.Trim()
                p.Phone = txtPhone.Text.Trim()
                p.Fax = txtFax.Text.Trim()
                p.Email = txtEmail.Text.Trim()
                p.WebSite = txtWebsite.Text.Trim()
                p.UserId = hidUserId.Value()
                p.Creator = Session("UserName")
                p.Created = Now
                data.Hotels.AddObject(p)
                data.SaveChanges()
                Insert_App_Log("Insert Khách sạn:" & txtHotelname.Text.Trim & "", Function_Name.Hotel, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                Excute_Javascript("Alertbox('Thêm mới dữ liệu thành công.');window.location ='../../Page/Hotel/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

    Protected Sub hplUser_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplUser.Init
        hplUser.CssClass = "thickbox"
        hplUser.NavigateUrl = "~/Page/Users/UserSelect.aspx?keepThis=true&TB_iframe=true&height=450&width=500&modal=true"
    End Sub

    Protected Sub ddlDistrict_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDistrict.SelectedIndexChanged
        Using _data As New VSEntities
            'huyen
            Dim _parentId As Integer = (From q In _data.Provinces Where q.Code = ddlDistrict.SelectedValue Select q.Id).FirstOrDefault
            Dim lstPhuongXa = (From q In _data.Provinces Where q.ParentId = _parentId Order By q.Title Select q.Id, q.Code, q.Title).ToList
            With ddlWard
                .DataSource = lstPhuongXa
                .DataTextField = "Title"
                .DataValueField = "Code"
                .DataBind()
                Dim itm As New ListItem("--- Chọn ---", "")
                .Items.Insert(0, itm)
            End With
       
        End Using
    End Sub
End Class


